<!--
 * @Description: 
 * @Author: Aroma
 * @Date: 2024-11-15 14:43:58
 * @LastEditors: Aroma
 * @LastEditTime: 2024-11-15 17:19:49
 * @别瞎看: 你个无脑牛马
-->
<script setup>
import { Delete, Edit, Search, Share, Upload, ArrowLeft, ArrowRight } from '@element-plus/icons-vue'
import { Eleme } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'

const title = ref('编辑')
const tableData = ref([
  { num: '001', name: '张三', score: '90', comment: '很优秀' },
  { num: '002', name: '李四', score: '92', comment: '优秀' },
  { num: '003', name: '王五', score: '83', comment: '很努力，不错' },
  { num: '004', name: '赵六', score: '98', comment: '666' },
])

const form = reactive({
  num: '',
  name: '',
  score: '',
  comment: '',
})

const dialogFormVisible = ref(false)
const selectedRow = ref(null)

const handleClick = (row) => {
  const index = tableData.value.findIndex(item => item.num === row.num)
  if (index !== -1) {
    tableData.value.splice(index, 1)
  }
}

const handleEdit = (row) => {
  title.value = '编辑';
  Object.assign(form, { ...row })
  selectedRow.value = row
  dialogFormVisible.value = true
}

const handleAdd = () => {
  title.value = '添加';
  Object.assign(form, {
    num: '',
    name: '',
    score: '',
    comment: '',
  })
  dialogFormVisible.value = true
}

const handleSearch = () => {
  const foundObject = tableData.value.find(item => item.num === searchStr.value);
  console.log("🚀 ~ handleSearch ~ foundObject:", foundObject)
  console.log("🚀 ~ handleSearch ~ handleSearch:", searchStr.value)
  tableData.value=[];
  tableData.value.push(foundObject);

}
const handleConfirm = () => {
  if (selectedRow.value) {
    // 编辑
    const index = tableData.value.findIndex(item => item.num === selectedRow.value.num)
    if (index !== -1) {
      tableData.value[index] = { ...form }
    }
  }else {
    // 新增
    tableData.value.push({ ...form })
  }
  dialogFormVisible.value = false
}

const searchStr = ref('')

</script>

<template>
<div class="main">
  <el-button type="primary" size="small" @click="handleAdd()">添加</el-button>
  <input type=" text" v-model="searchStr" >
  <el-button type="primary" size="small" @click="handleSearch()">搜索</el-button>
  <el-table :data="tableData" style="width: 100%;">
    <el-table-column fixed prop="num" label="学号" width="150" />
    <el-table-column prop="name" label="姓名" width="120" />
    <el-table-column prop="score" label="分数" width="120" />
    <el-table-column prop="comment" label="评语" width="120" />
    <el-table-column fixed="right" label="操作" min-width="120">
      <template #default="scope">
        <el-button link type="danger" size="small" @click="handleClick(scope.row)">删除</el-button>
        <el-button link type="warning" size="small" @click="handleEdit(scope.row)">编辑</el-button>
      </template>
    </el-table-column>
  </el-table>

  <el-dialog v-model="dialogFormVisible" :title="title" width="500">
    <el-form :model="form">
      <el-form-item label="学号" :label-width="formLabelWidth">
        <el-input v-model="form.num" autocomplete="off" />
      </el-form-item>
      <el-form-item label="姓名" :label-width="formLabelWidth">
        <el-input v-model="form.name" autocomplete="off" />
      </el-form-item>
      <el-form-item label="分数" :label-width="formLabelWidth">
        <el-input v-model="form.score" autocomplete="off" />
      </el-form-item>
      <el-form-item label="评语" :label-width="formLabelWidth">
        <el-input v-model="form.comment" autocomplete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog_footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确定</el-button>
      </div>
    </template>
     
  </el-dialog>

  

</div>
</template>

<style scoped>
.main {
  padding: 20%;
}

</style>